<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { queryPageApi } from '@/api/car'

const searchCar = ref({
  brand: '',
  userName: '',
  color: ''
})

onMounted(async () => {
  handleSearch()
})

//查询车辆
const handleSearch = async () => {
  console.log('Search:', searchCar.value)
  // 修改API调用，尝试将颜色参数传递给后端
  const result = await queryPageApi(searchCar.value.brand, searchCar.value.userName, searchCar.value.color, currentPage.value, pageSize.value);
  if(result.code){
    console.log('API返回数据结构:', result.data.rows[0]) // 调试API返回的数据结构
    carList.value = result.data.rows
    total.value = result.data.total
  }
}

const handleReset = () => {
  // 清空表单
  searchCar.value = {
    brand: '',
    userName: '',
    color: ''
  }
  handleSearch()
}

// 示例数据
const carList = ref([])

// 分页配置
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(0)

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<template>
  <h1>车辆查询</h1> <br>
  <el-form :inline="true" :model="searchCar">
    <el-form-item label="品牌">
      <el-input v-model="searchCar.brand" placeholder="请输入车辆品牌"></el-input>
    </el-form-item>

    <el-form-item label="用户名">
      <el-input v-model="searchCar.userName" placeholder="请输入用户姓名"></el-input>
    </el-form-item>
    
    <el-form-item label="颜色">
      <el-input v-model="searchCar.color" placeholder="请输入车辆颜色"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">清空</el-button>
    </el-form-item>
  </el-form>

  <!-- 表格 -->
  <el-table :data="carList" border style="width: 100%">
    <el-table-column type="index" label="序号" width="70" align="center"/>
    <el-table-column prop="brand" label="品牌" width="120" align="center"></el-table-column>
    <el-table-column prop="model" label="型号" width="120" align="center"></el-table-column>
    <el-table-column prop="color" label="颜色" width="80" align="center">
      <template #default="scope">
        {{ scope.row.color || '未设置' }}
      </template>
    </el-table-column>
    <el-table-column prop="plateNumber" label="车牌号" width="100" align="center"></el-table-column>
    <el-table-column prop="remark" label="备注" width="320" align="center"></el-table-column>
    <el-table-column prop="year" label="生产年份" width="120" align="center"></el-table-column>
    <el-table-column prop="userName" label="用户姓名" width="120" align="center"></el-table-column>
    <el-table-column prop="updateTime" label="最后操作时间" align="center"></el-table-column>
  </el-table>
  <br>

  <!-- 分页 -->
  <el-pagination
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="[5, 10, 20, 30]"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  >
  </el-pagination>
</template>

<style scoped>
.el-form-item {
  margin-bottom: 16px;
}
</style>